﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;

namespace SNSpirit.Documents.Adatpers.Ubb.Internal
{
	/// <summary>
	/// Represent as an style stack.
	/// </summary>
	internal class ElementStyleStack : IEnumerable<ElementStyle>
	{
		/// <summary>
		/// Internal style stack.
		/// </summary>
		private Stack<ElementStyle> _StyleStack;

		/// <summary>
		/// Push a new style into stack.
		/// </summary>
		/// <param name="style">The new style to be pushed.</param>
		public void Push(ElementStyle style)
		{
			if (style == null)
			{
				throw new ArgumentNullException("style");
			}

			Contract.EndContractBlock();

			this._StyleStack.Push(style);
		}

		/// <summary>
		/// Pop the lastest style.
		/// </summary>
		/// <returns>The lastest style poped.</returns>
		public ElementStyle Pop()
		{
			return this._StyleStack.Pop();
		}

		/// <summary>
		/// Push a new sytle into current context and pop it when disposing.
		/// </summary>
		/// <param name="style">The style to be pushing.</param>
		/// <returns>A object can be disposed, and the style will be poped when this object is disposing.</returns>
		public IDisposable PushContext(ElementStyle style)
		{
			if (style == null)
			{
				throw new ArgumentNullException("style");
			}

			Contract.EndContractBlock();

			return new ElementStylePushContext(this, style);
		}

		/// <summary>
		/// Initialize a new empty style stack.
		/// </summary>
		public ElementStyleStack()
		{
			this._StyleStack = new Stack<ElementStyle>();
		}

		/// <summary>
		/// Enumerate all styles in this stack.
		/// </summary>
		/// <returns>An object to enumerate styles in this stack.</returns>
		public IEnumerator<ElementStyle> GetEnumerator()
		{
			return this._StyleStack.GetEnumerator();
		}

		/// <summary>
		/// Enumerate all styles in this stack.
		/// </summary>
		/// <returns>An object to enumerate styles in this stack.</returns>
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.GetEnumerator();
		}
	}
}
